package com.xmtlxx.wms.domain.vo;

import com.xmtlxx.common.utils.DateUtils;

import java.util.*;

public class AsynInfo {
    // 用于存储所有的任务进度信息
    public static Map<String, AsynInfo> allAsynInfo = new HashMap<String, AsynInfo>();

    // 提示信息或 异常信息
    private String msg;
    // 数据总数
    private Integer totality = 0;
    // 已处理的数据条数
    private Integer doneSum = 0;
    // 失败的数据条数
    private Integer errorSum = 0;
    // 成功的数据条数
    private Integer successSum = 0;
    // 错误文件的路径
    public String errorFilePath;
    // 是否结束
    public Boolean isEnd = false;

    public String key = null;

    //当前处理任务
    public String current;

    public String startTime;

    public String endTime;

    public String title;

    public List<PutDtlVo> objs;//任务对象

    /**
     * 创建一个进度信息,并获取对应的uuid
     *
     * @return
     */
    public static String createAsynInfo() {
        AsynInfo asynInfo = new AsynInfo();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        allAsynInfo.put(uuid, asynInfo);
        return uuid;
    }

    /**
     * 创建一个uuid进度信息
     *
     * @return
     */
    public static boolean createAsynInfoByUUID(String uuid, PutDtlVo obj, String title) {
        AsynInfo info = getAsynInfo(uuid);
        obj.setUuid(uuid);
        if( info != null && !info.isEnd){//存在 且 未结束
            List<PutDtlVo> objs = info.getObjs();
            List<PutDtlVo> newArrs = new ArrayList<>();
            Boolean had = false;
            if(objs != null && objs.size() > 0){
                for(int i=0;i<objs.size();i++){
                    PutDtlVo item = objs.get(i);
                    if(item.getPno().equals(obj.getPno())){
                        newArrs.add(obj);
                        had = true;
                    }else{
                        newArrs.add(item);
                    }
                }
            }
            if(!had){
                newArrs.add(obj);
            }

            info.setObjs(newArrs);
        }else{
            info = new AsynInfo();
            List<PutDtlVo> newArrs = new ArrayList<>();
            newArrs.add(obj);
            info.setObjs(newArrs);
            info.setKey(uuid);
            info.setTitle(title);
            info.setStartTime(DateUtils.date2String(new Date()));
        }
        if(info.getObjs() != null && info.getObjs().size() > 0){
            info.setTotality(info.getObjs().size());
        }else{
            info.setTotality(0);
        }
        allAsynInfo.put(uuid, info);
        return true;
    }

    /**
     * 通过uuid获取进度信息
     *
     * @param uuid
     * @return
     */
    public static AsynInfo getAsynInfo(String uuid) {
        return allAsynInfo.get(uuid);
    }

    /**
     * 通过uuid删除对应的进度信息
     *
     * @param uuid
     * @return
     */
    public static void deleteAsynInfo(String uuid) {
        allAsynInfo.remove(uuid);
    }

    public static void deleteAsynInfoItem(String uuid,PutDtlVo item) {
        AsynInfo info = getAsynInfo(uuid);
        List<PutDtlVo> objs = info.getObjs();
        List<PutDtlVo> newArrs = new ArrayList<>();
        item.setUuid(uuid);
        if(objs != null && objs.size() > 0){
            for(int i=0;i<objs.size();i++){
                PutDtlVo jsonObj = objs.get(i);
                if(!jsonObj.getPno().equals(item.getPno())){
                    newArrs.add(jsonObj);
                }
            }
        }
        info.setObjs(newArrs);
        if(info.getObjs() != null && info.getObjs().size() > 0){
            info.setTotality(info.getObjs().size());
        }else{
            info.setTotality(0);
        }
        allAsynInfo.put(uuid, info);
    }

    public static void updateAsynInfoItem(String uuid,PutDtlVo item) {
        AsynInfo info = getAsynInfo(uuid);
        List<PutDtlVo> objs = info.getObjs();
        List<PutDtlVo> newArrs = new ArrayList<>();
        item.setUuid(uuid);
        if(objs != null && objs.size() > 0){
            for(int i=0;i<objs.size();i++){
                PutDtlVo jsonObj = objs.get(i);
                if(jsonObj.getPno().equals(item.getPno())){
                    newArrs.add(item);
                }else{
                    newArrs.add(jsonObj);
                }
            }
        }
        info.setObjs(newArrs);
        if(info.getObjs() != null && info.getObjs().size() > 0){
            info.setTotality(info.getObjs().size());
        }else{
            info.setTotality(0);
        }
        allAsynInfo.put(uuid, info);
    }

    /**
     * uuid对应的进度 已处理的数据条数+1
     *
     * @param uuid
     */
    public static void doneSumAddOne(String uuid) {
        AsynInfo asynInfo = getAsynInfo(uuid);
        asynInfo.setDoneSum(asynInfo.getDoneSum() + 1);
    }

    /**
     * uuid对应的进度 失败的数据条数+1
     *
     * @param uuid
     */
    public static void errorSumAddOne(String uuid) {
        AsynInfo asynInfo = getAsynInfo(uuid);
        asynInfo.setErrorSum(asynInfo.getErrorSum() + 1);
    }

    /**
     * uuid对应的进度 成功的数据条数+1
     *
     * @param uuid
     */
    public static void successSumAddOne(String uuid) {
        AsynInfo asynInfo = getAsynInfo(uuid);
        asynInfo.setSuccessSum(asynInfo.getSuccessSum() + 1);
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getTotality() {
        return totality;
    }

    public void setTotality(Integer totality) {
        this.totality = totality;
    }

    public Integer getDoneSum() {
        return doneSum;
    }

    public void setDoneSum(Integer doneSum) {
        this.doneSum = doneSum;
    }

    public Integer getErrorSum() {
        return errorSum;
    }

    public void setErrorSum(Integer errorSum) {
        this.errorSum = errorSum;
    }

    public Integer getSuccessSum() {
        return successSum;
    }

    public void setSuccessSum(Integer successSum) {
        this.successSum = successSum;
    }

    public String getErrorFilePath() {
        return errorFilePath;
    }

    public void setErrorFilePath(String errorFilePath) {
        this.errorFilePath = errorFilePath;
    }

    public Boolean getEnd() {
        return isEnd;
    }

    public void setEnd(Boolean end) {
        isEnd = end;
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public List<PutDtlVo> getObjs() {
        return objs;
    }

    public void setObjs(List<PutDtlVo> objs) {
        this.objs = objs;
    }

    public String getCurrent() {
        return current;
    }

    public void setCurrent(String current) {
        this.current = current;
    }

    public String getStartTime() {
        return startTime;
    }

    public void setStartTime(String startTime) {
        this.startTime = startTime;
    }

    public String getEndTime() {
        return endTime;
    }

    public void setEndTime(String endTime) {
        this.endTime = endTime;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }


}
